package com.example.spider.provider.why10w.deprecated.why10org;

import cn.hutool.core.lang.PatternPool;
import cn.hutool.core.util.ReUtil;
import com.example.spider.provider.why10w.AbstractWhy10wListResultHandler;
import com.example.spider.provider.why10w.deprecated.why10org.task.Why10orgDetailTask;
import com.example.spider.provider.why10w.dto.Why10wListTask;
import com.example.spider.task.SpiderTaskQueue;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 处理列表查询结果
 *
 * @author lym
 */
@Slf4j
//@Component
public class Why10orgListResultHandler extends AbstractWhy10wListResultHandler {

    private static final String DOMAIN_URL = "http://www.10why.org/";

    @Override
    public void startSpiders() {
        for (Why10orgCategoryEnum categoryEnum : Why10orgCategoryEnum.values()) {
            Why10wListTask listTask = createPageTask(categoryEnum.getFirstCategory(), categoryEnum.getSecondCategory(), 1);
            SpiderTaskQueue.putTask(listTask);
        }
    }

    protected String buildPageTaskUrl(String firstCategory, String secondCategory, int pageNo) {
        String baseUrl = DOMAIN_URL + firstCategory + "/" + secondCategory;
        return pageNo == 1 ? baseUrl : baseUrl + "/page/" + pageNo;
    }

    /**
     * 分析列表结果，创建下载详情任务
     */
    @Override
    public void doHandle(Why10wListTask task, String html) {
        String hasContentRegex = "id=\"f\"(.*?)id=\"g\"";
        List<String> contentStr = ReUtil.findAllGroup1(hasContentRegex, html);
        if (contentStr.isEmpty()) {
            // 爬完了都，会返回404页面，是没有内容的
            log.error("not a valid result. May finished!");
            return;
        }
        String content = contentStr.get(0);
        String urlAndTitleRegex = "<h2>.*?<a href=\"(.*?)\".*?title=\".*?\">(.*?)</a>";
        Pattern urlAndTitlePattern = PatternPool.get(urlAndTitleRegex, 32);

        // 一页大概10项
        List<Why10orgDetailTask> detailTaskList = new ArrayList<>(15);
        ReUtil.findAll(urlAndTitlePattern, content, m -> {
            Why10orgDetailTask detailTask = new Why10orgDetailTask()
                    .setTitle(m.group(2))
                    .setUrl(m.group(1));
            detailTaskList.add(detailTask);
        });


        if (CollectionUtils.isEmpty(detailTaskList)) {
            log.warn("items is empty");
            return;
        }
        // 创建爬取详情的任务
        detailTaskList.forEach(SpiderTaskQueue::putTask);

        // 创建爬取下一页的任务
        boolean hasNextPage = ReUtil.contains("class='inactive' >\\d+?</a></div>", html);
        if(hasNextPage) {
            Why10wListTask nextPageTask = createPageTask(task.getFirstCategory(),
                    task.getSecondCategory(), task.getPage() + 1);
            SpiderTaskQueue.putTask(nextPageTask);
        }

        repository.saveHtml(task, html);
    }
}
